package com.example.stugradeapp

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.util.zip.Inflater

//class FruitAdapter(val fruitList: List<Fruit>) :
//RecyclerView.Adapter<FruitAdapter.ViewHolder>()
class CustomeAdapter(val context:Context,val book_id:ArrayList<Int>,
                     val book_title:ArrayList<String>,
                     val book_author:ArrayList<String>,
                     val book_pages:ArrayList<Int>)
    :RecyclerView.Adapter<CustomeAdapter.MyViewHolder>() {
    //inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
    inner class MyViewHolder(view: View):RecyclerView.ViewHolder(view){
        val book_id_text:TextView = itemView.findViewById(R.id.book_id_text)
        val book_title_text:TextView = itemView.findViewById(R.id.book_title_text)
        val book_author_text:TextView = itemView.findViewById(R.id.book_author_text)
        val book_pages_text:TextView = itemView.findViewById(R.id.book_pages_text)
        val mainLayout:LinearLayout = itemView.findViewById(R.id.mainLayout)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.my_row,parent,false
        )
        return MyViewHolder(view)

    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.book_id_text.text = book_id[position].toString()
        holder.book_title_text.text = book_title[position]
        holder.book_author_text.text = book_author[position]
        holder.book_pages_text.text = book_pages[position].toString()

        holder.mainLayout.setOnClickListener{
            val intent = Intent(context,updateActivity::class.java)
            intent.putExtra("id",book_id[position].toString())
            intent.putExtra("title",book_title[position])
            intent.putExtra("author",book_author[position])
            intent.putExtra("pages",book_pages[position].toString())
            context.startActivity(intent)
        }
    }

    override fun getItemCount()=book_id.size
}